package com.player.rest.util;

import java.util.regex.Pattern;

import com.player.entity.RankinglistType;

public class InputValidator {

	/**
	 * <pre>
	 * ^                    	# Start of the line
	 *   [a-z0-9_-]	     		# Match characters and symbols in the list, a-z, 0-9 , underscore , hyphen
	 *              {3,30}  	# Length at least 3 characters and maximum length of 30 
	 * $                    	# End of the line
	 * </pre>
	 */
	private static final String PLAYERNAME_PATTERN = "^[a-z0-9_-]{3,30}$"; // FIXME regex playername: begin with letter

	/**
	 * <pre>
	 * ^						#start of the line
	 * 	  [_A-Za-z0-9-]+		#  must start with string in the bracket [ ], must contains one or more (+)
	 * 	  (						#  start of group #1
	 * 	    \\.[_A-Za-z0-9-]+	#     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
	 * 	  )*					#  end of group #1, this group is optional (*)
	 * 	    @					#     must contains a "@" symbol
	 * 	     [A-Za-z0-9]+       #        follow by string in the bracket [ ], must contains one or more (+)
	 * 	      (					#	   start of group #2 - first level TLD checking
	 * 	       \\.[A-Za-z0-9]+  #	     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
	 * 	      )*				#	   end of group #2, this group is optional (*)
	 * 	      (					#	   start of group #3 - second level TLD checking
	 * 	       \\.[A-Za-z]{2,}  #	     follow by a dot "." and string in the bracket [ ], with minimum length of 2
	 * 	      )					#	   end of group #3
	 * 	$						#end of the line
	 * </pre>
	 */
	@SuppressWarnings("unused")
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private static final String GMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@(gmail.com|googlemail.com)$";

	public static void validatePlayername(final String playername, final String paramName, final ValidationResult result) {
		if (!validate(playername, PLAYERNAME_PATTERN)) {
			result.addMessage(paramName, "Must be 3-30 characters long and must not contain any special characters.");
		}
	}

	public static void validateEmail(final String email, final String formParamName, final ValidationResult result) {
		if (!validate(email, GMAIL_PATTERN)) {
			result.addMessage(formParamName,
					"Must be a valid Google Mail address (e.g. @gmail.com or @googlemail.com).");
		}
	}

	public static void validateRankinglist(final String rankinglist, final String formParamName,
			final ValidationResult result) {
		try {
			RankinglistType.valueOf(rankinglist);
		} catch (IllegalArgumentException e) {
			result.addMessage(formParamName, "Please choose a valid ranking list.");
		}
	}

	public static void validatePlayerId(final String playerId, final String formParamName, final ValidationResult result) {
		try {
			Long.parseLong(playerId);
		} catch (NumberFormatException e) {
			result.addMessage(formParamName, "Invalid id \"" + playerId + "\"");
		}
	}

	private static boolean validate(String s, String pattern) {
		Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
		return p.matcher(s).matches();
	}

}
